﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MHYS.DEMO.Model;
using MEYS.DEMO.BLL;
namespace Demo
{
    public partial class ShopCar : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

            if (this.Session["ShopCar"] == null)
            {
                return;
            }
            if (!this.IsPostBack)
            {
                BindData();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            List<OrderItem> orderItemsList = this.Session["ShopCar"] as List<OrderItem>;
            orderItemsList = ComputeMoney(orderItemsList);
            this.gvCar.DataSource = orderItemsList;
            this.gvCar.DataBind();
        }

        /// <summary>
        /// 计算集合中每个订单金钱
        /// </summary>
        /// <param name="orderItemsList"></param>
        private static List<OrderItem> ComputeMoney(List<OrderItem> orderItemsList)
        {
            foreach (var item in orderItemsList)
            {
                orderItemsList.Find(m => m.Product.GName == item.Product.GName).PayMoney = item.Product.Price * item.Num;
            }
            return orderItemsList;
        }

        protected void gvCar_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvCar_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<OrderItem> orderItemList = this.Session["ShopCar"] as List<OrderItem>;
            if (orderItemList.Count != 0)
            { //购物车中没指定删除的物品 
                foreach (var item in orderItemList)
                {
                    if (orderItemList.Find(m => m.Product.GName == this.gvCar.Rows[e.RowIndex].Cells[1].Text).Num == 0)
                    {//购物车中没有指定商品
                        return;
                    }
                    if (item.Product.GName == this.gvCar.Rows[e.RowIndex].Cells[1].Text)
                    {
                        if (item.Num > 1)
                        {
                            orderItemList.Find(m => m.Product.GName == item.Product.GName).Num--;
                        }
                        else
                        {
                            orderItemList.RemoveAll(m => m.Product.GName == this.gvCar.Rows[e.RowIndex].Cells[1].Text);
                            //重新绑定数据
                            this.Session["ShopCar"] = ComputeMoney(orderItemList);
                            this.gvCar.DataSource = this.Session["ShopCar"] as List<OrderItem>;
                            this.gvCar.DataBind();
                            return;
                        }
                    }
                }
                //重新绑定数据
                this.Session["ShopCar"] = ComputeMoney(orderItemList);
                this.gvCar.DataSource = this.Session["ShopCar"] as List<OrderItem>;
                this.gvCar.DataBind();
            }
        }

        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPay_Click(object sender, EventArgs e)
        {
            try
            {
                List<OrderItem> orderItemList = this.Session["ShopCar"] as List<OrderItem>;
                //创建订单实体
                Order order = new Order();
                //实例化OrderItem对象
                order.OrderItem = new List<OrderItem>();
                //添加订单实体的信息
                foreach (var item in orderItemList)
                {
                    order.OrderItem.Add(item);
                    order.OrderMoney += item.PayMoney;
                    order.OrderTime = DateTime.Now;
                    order.UserID = 2;
                }
                if (OrderManager.AddOrder(order))
                {
                    this.Response.Write("<script>alert('支付成功')</script>");
                }
                else
                {
                    this.Response.Write("<script>alert('支付失败')</script>");
                }
            }
            catch (Exception ex)
            {
                this.Response.Write("<script>alert('支付失败')</script>");
            }
        }


    }
}